//
//  WBHomeViewController.swift
//  swift微博
//
//  Created by qlwl on 17/2/25.
//  Copyright © 2017年 qlwl. All rights reserved.
//

import UIKit

//用来定义全局变量 ，一般加上private
private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    //微博数据数组
    lazy fileprivate var statusList = [String]()
    
    ///加载数据
    override func loadData(){
        //模拟延时加载数据
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            for i in 0..<20 {
                if self.isPullup{
                    //上拉加载数据
                    self.statusList.append("上拉\(i)")
                }else{
                    self.statusList.insert(i.description, at: 0)
                    // 下拉刷新数据
                }
            }
            
            print("刷新表格")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            
            self.tableView?.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.orange
    }
    @objc fileprivate func showFriends (){
        print(#function)
        let vc = WBDemoViewController()
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }
}

//MARK - 表格数据源方法, 具体的数据源实现，不需要super
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId , for: indexPath)
        
        cell.textLabel?.text = statusList[indexPath.row]
        
        return cell
    }
    
}

extension WBHomeViewController {
    ///重写父类方法
    override func setupUI(){
        super.setupUI()
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        //
//        let btn: UIButton = UIButton.yw_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, targert: self, action: #selector(showFriends))
        
//        let person = Person()
//        person.name = "小花"
//        
//        let label = DemoLabel(frame: CGRect(x: 0, y: 100, width: 100, height: 40))
//        label.person = person
//        view.addSubview(label)
        
        // 注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
    }
}
